package com.gec.controller;

import com.gec.dao.SysUserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/RegisterServlet")

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String phone=req.getParameter("phone");
        String address=req.getParameter("address");
        if(username.length()!=0&&password.length()!=0&&phone.length()!=0&&address.length()!=0) {
            String sql = "insert into sys_user values(null,?,?,?,?)";
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(username);
            objects.add(password);
            objects.add(phone);
            objects.add(address);
            SysUserDao sysUserDao = new SysUserDao();
            sysUserDao.update(sql, objects);
            String success = "注册成功....\n正在跳转页面...\n请稍后！";
            JOptionPane.showMessageDialog(null, success);
            resp.sendRedirect("login.jsp");
        }else{
            String fail="请按要求填写注册信息或注册信息未完善....\n请完善您的注册信息!";
            JOptionPane.showMessageDialog(null, fail);
            resp.sendRedirect("register.jsp");
        }
    }
}
